/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sec.rbac.db;

import han.bifang.business.dao.GroupDao;
import han.bifang.business.dao.GroupRoleRelDao;
import han.bifang.business.dao.ResourceDao;
import han.bifang.business.dao.RoleDao;
import han.bifang.business.dao.RoleResourceRelDao;
import han.bifang.business.sec.rbac.RoleProvider;
import han.bifang.business.sync.SyncUtils;
import han.bifang.common.entity.SoaRole;
import han.bifang.common.entity.SoaRoleResRel;
import han.bifang.common.entity.SoaRoleResRelPK;
import han.bifang.common.entity.SoaUsergroup;
import han.bifang.common.entity.SoaUsergroupRoleRel;
import han.bifang.common.entity.SoaUsergroupRoleRelPK;
import han.bifang.common.exception.BifangException;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Kent
 */
public class DbRoleProvider implements RoleProvider {

	@Override
	public void addGroupsToRole(String[] groupIds, String roleId)
			throws BifangException {
		ArrayList<SoaUsergroupRoleRel> l = new ArrayList<SoaUsergroupRoleRel>();
		for (String id : groupIds) {
			SoaUsergroupRoleRel r = new SoaUsergroupRoleRel();
			SoaUsergroupRoleRelPK rk = new SoaUsergroupRoleRelPK();
			rk.setRoleId(roleId);
			rk.setUsergroupId(id);
			r.setCompId(rk);
			l.add(r);
		}
		GroupRoleRelDao.createUsergroupRoleRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public String createRole(SoaRole role) throws BifangException {
		return RoleDao.createRole(role);
	}

	@Override
	public List findRolesByPage(String roleName, int page, int pageSize)
			throws BifangException {
		return RoleDao.findRolesByPage(roleName, page, pageSize);
	}

	@Override
	public List<SoaUsergroup> getGroupsOfRole(String roleId)
			throws BifangException {
		return GroupDao.findGroupsByRoleId(roleId);
	}

	@Override
	public List getResourcesOfRoleByPage(String roleId, int page, int pageSize)
			throws BifangException {
		return ResourceDao.getResourcesOfRoleByPage(roleId, page, pageSize);
	}

	public List getResourcesNotOfRoleByPage(String roleId, String resourceType,
			String property1, String property2, String property3, int page,
			int pageSize) throws BifangException {
		return ResourceDao.getResourcesNotOfRoleByPage(roleId,resourceType, property1,
				property2, property3, page, pageSize);
	}

	@Override
	public void modifyRole(SoaRole role) throws BifangException {
		RoleDao.modifyRole(role);
		SyncUtils.sync(SyncUtils.SECURITY, null);
	}

	@Override
	public void removeGroupsFromRole(String[] groupIds, String roleId)
			throws BifangException {
		ArrayList<SoaUsergroupRoleRel> l = new ArrayList<SoaUsergroupRoleRel>();
		for (String id : groupIds) {
			SoaUsergroupRoleRel r = new SoaUsergroupRoleRel();
			SoaUsergroupRoleRelPK rk = new SoaUsergroupRoleRelPK();
			rk.setRoleId(roleId);
			rk.setUsergroupId(id);
			r.setCompId(rk);
			l.add(r);
		}
		GroupRoleRelDao.removeUsergroupRoleRel(l);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public void removeRole(String roleId) throws BifangException {
		RoleDao.removeRole(roleId);
		GroupRoleRelDao.removeUsergroupRoleRelByRoleId(roleId);
		RoleResourceRelDao.removeRoleResRelByRoleId(roleId);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

	@Override
	public SoaRole getRoleById(String id) throws BifangException {
		return RoleDao.getRole(id);
	}

	@Override
	public SoaRole getRoleByName(String roleName) throws BifangException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getGroupsNotOfRoleByPage(String roleId, String groupName,
			int page, int pageSize) throws BifangException {
		return GroupDao.findGroupsNotOfRoleByPage(roleId, groupName, page,
				pageSize);
	}

	@Override
	public void addResourcesToRole(String[] resourceIds, String roleId)
			throws BifangException {
		ArrayList<SoaRoleResRel> r = new ArrayList<SoaRoleResRel>();
		for (String id : resourceIds) {
			SoaRoleResRel s = new SoaRoleResRel();
			SoaRoleResRelPK sk = new SoaRoleResRelPK();
			sk.setResId(id);
			sk.setRoleId(roleId);
			s.setCompId(sk);
			r.add(s);
		}
		RoleResourceRelDao.createRoleResRel(r);

	}

	@Override
	public void removeResourcesFromRole(String[] resourceIds, String roleId)
			throws BifangException {
		ArrayList<SoaRoleResRel> r = new ArrayList<SoaRoleResRel>();
		for (String id : resourceIds) {
			SoaRoleResRel s = new SoaRoleResRel();
			SoaRoleResRelPK sk = new SoaRoleResRelPK();
			sk.setResId(id);
			sk.setRoleId(roleId);
			s.setCompId(sk);
			r.add(s);
		}
		RoleResourceRelDao.removeRoleResRel(r);
		SyncUtils.sync(SyncUtils.SECURITY, null);

	}

}
